package studentManager2;

import java.util.Arrays;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class StuModel extends AbstractTableModel {
	Vector rowData;
	Vector columnNames = new Vector(Arrays.asList("学号", "名字", "性别", "年龄", "籍贯", "门派"));
	private final static String ADDSQL = "insert into stu values(?,?,?,?,?,?)";
	private final static String DELSQL = "delete from stu where id=?";
	private final static String UPDSQL = "update stu set name=?,sex=?,age=?,jiGuan=?,dept=? where id=?";
	private final static String SELSQL = "select top 10 * from stu where name like '%'+?+'%'";
	static String sql2 = "select top 10 * from stu where id not in (select top 10 id from stu)";

	private static StuModel sm = new StuModel();

	private StuModel() {
	}

	@Override
	public int getRowCount() {
		return rowData.size();
	}

	@Override
	public int getColumnCount() {
		return columnNames.size();
	}

	@Override
	public String getValueAt(int rowIndex, int columnIndex) {
		return ((Vector) rowData.get(rowIndex)).get(columnIndex) + "";
	}

	@Override
	public String getColumnName(int column) {
		return (String) columnNames.get(column);
	}

	public static boolean addStu(Object... args) {
		return DBUtil.executeUpdate(ADDSQL, args) == 1;
	}

	public static boolean delStu(Object... args) {
		return DBUtil.executeUpdate(DELSQL, args) == 1;
	}

	public static boolean updStu(Object... args) {
		return DBUtil.executeUpdate(UPDSQL, args) == 1;
	}

	public static StuModel selStu(Object... args) {
		sm.rowData = DBUtil.select(SELSQL, args);
		return sm;
	}

	public static StuModel pStu(Object... args) {
		sm.rowData = DBUtil.select(sql2, args);
		return sm;
	}
}
